package com.schoolCommunity.controller;


import com.alibaba.fastjson.annotation.JSONField;
import com.schoolCommunity.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.net.www.content.image.jpeg;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://SCHOOLCOMMUNITY-LOGIN";

    //负载均衡测试用
    @GetMapping("/LoginHello")
    public String hello() {
        System.out.println("-------------------进入了测试--------------");
        return restTemplate.getForObject(REST_URL_PREFIX + "/hello", String.class);
    }

    //返回验证码给前端
    //使用7150接口
//    @RequestMapping("/kaptcha")
//    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        restTemplate.getForObject(REST_URL_PREFIX+"/kaptcha", HttpServletResponse.class);
//    }

    //单独校验邮箱验证码
    @GetMapping("checkEmailSignIn/{email}/{EmailSignIn}")
    public Information checkEmailSignIn(@PathVariable("email") String email,
                                        @PathVariable("EmailSignIn") String EmailSignIn) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/checkEmailSignIn/" + email + "/" + EmailSignIn, Information.class);
    }

    //注册，添加到member表
    @PostMapping("/InsertMember")
    public Information<Member> InsertMember(@RequestBody Member Member) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/InsertMember", Member, Information.class);
    }

    //登录
    @PostMapping("/LoginMember")
    public Information<Member> LoginMember(@RequestBody Member Member) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/LoginMember", Member, Information.class);
    }

    //获取注册邮箱验证码
    //参数为邮箱号码与图形验证码
    @PostMapping("/getSignInEmail/{email}/{kaptcha}")
    public Information getPassEmail(@PathVariable("email") String email,
                                    @PathVariable("kaptcha") String kaptcha,
                                    HttpServletRequest request) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/getSignInEmail/" + email + "/" + kaptcha, null, Information.class);
    }

    //发送找回密码邮箱验证码
    @PostMapping("/getPassBackEmail/{email}/{kaptcha}")
    public Information getPassBackEmail(@PathVariable("email") String email,
                                        @PathVariable("kaptcha") String kaptcha,
                                        HttpServletRequest request) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/getPassBackEmail/" + email + "/" + kaptcha, null, Information.class);
    }

    //校验找回密码的邮箱验证码，直接修改密码
    @PostMapping("/changePass/{EmailGetPass}")
    public Information checkAndChangePass(@PathVariable("EmailGetPass") String EmailGetPass, @RequestBody Member Member) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/changePass/" + EmailGetPass, Member, Information.class);
    }

    //写入一张图片
    //取巧，使用7150的
    @PostMapping("/UploadPicture/{memberId}")
    public Information UploadPicture(@RequestParam("file") MultipartFile file, @PathVariable("memberId") long memberId) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/UploadPicture/" + memberId, file, Information.class);
    }

    //删除指定用户的一张图片
    @GetMapping("/DeletePicture/{memberId}")
    public Information DeletePicture(@PathVariable("memberId") long memberId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/DeletePicture/" + memberId, Information.class);
    }

    //管理员功能，根据ID删除用户
    @GetMapping("/DeleteMember/{memberId}")
    public Information DeleteMember(@PathVariable("memberId") long memberId) throws IOException {
        return restTemplate.getForObject(REST_URL_PREFIX + "/DeleteMember/" + memberId, Information.class);
    }

    //管理员功能，修改用户信用等级
    @GetMapping("/UpdateMemberCredit/{memberId}/{creditNumber}")
    public Information UpdateMemberCredit(@PathVariable("memberId") long memberId, @PathVariable("creditNumber") int creditNumber) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/UpdateMemberCredit/" + memberId + "/" + creditNumber, Information.class);
    }

    //管理员功能，获取所有用户
    @GetMapping("/GetAllMember")
    public Information GetAllMember() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/GetAllMember", Information.class);
    }

    //登出
    @PostMapping("/user/logout")
    public Information logout(@RequestHeader("X-Token") String token) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/user/logout", token, Information.class);
    }

    //userInfo
    @GetMapping("/user/info")
    public Information info(@RequestParam("token") String token) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/user/info", Information.class);
    }

    //测试后台登录
    @PostMapping("/user/login")
    public Information login(@RequestBody voUser voUser) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/user/login", voUser, Information.class);
    }

    //管理员登录
    @PostMapping("/adminLogin/{email}/{password}")
    public Information adminLogin(@PathVariable("email") String email, @PathVariable("password") String password) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/adminLogin/" + email + "/" + password, null, Information.class);
    }

    //管理员添加管理员
    @PostMapping("/insertAdmin")
    public Information insertAdmin(@RequestBody Admin admin) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/insertAdmin", admin, Information.class);
    }

    //管理员获取普通用户最大分页数
    @PostMapping("/getPagingMax")
    public Information getPagingMax() {
        return restTemplate.postForObject(REST_URL_PREFIX + "/getPagingMax", null, Information.class);
    }

    //管理员分页获取普通用户数据
    @GetMapping("/pagingGetAllMember/{count}")
    public Information pagingGetAllMember(@PathVariable("count") long count) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/pagingGetAllMember/" + count, Information.class);
    }

    //获取所有管理员信息
    @PostMapping("/getAllAdmin/{count}")
    public Information getAllAdmin(@PathVariable("count") long count) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/getAllAdmin/" + count, null, Information.class);
    }

    //获取管理员最大分页
    @PostMapping("/getAdminPagingMax")
    public Information getAdminPagingMax() {
        return restTemplate.postForObject(REST_URL_PREFIX + "/getAdminPagingMax", null, Information.class);
    }

    //删除管理员
    @PostMapping("/deleteAdmin/{deleteId}")
    public Information deleteAdmin(@PathVariable("deleteId") long deleteId) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/deleteAdmin/" + deleteId, null, Information.class);
    }

    //搜索用户，传入昵称，姓氏，页数
    @PostMapping("/searchMember/{pageNo}")
    public Information searchMember(@PathVariable("pageNo") int pageNo,
                                    @RequestBody voSearch voSearch
    ) throws IOException {
        return restTemplate.postForObject(REST_URL_PREFIX + "/searchMember/" + pageNo, voSearch, Information.class);

    }


}
